/*
 * jxSwing Project (2014)
 * 
 * by Denilson Edinaldo Pinto
 * 
 * LGPL
 * Copyright (C) 2014
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
package org.jxSwing.component.tree.node;

import java.util.Enumeration;

import javax.swing.tree.DefaultMutableTreeNode;

/**
 * @author Denilson Edinaldo Pinto
 * @porject jxSwing
 * @package org.jxSwing.component.tree
 * @date 15/02/2014 - 08:39:09
 */
public class CheckNode extends DefaultMutableTreeNode {
	
	public final static int DIG_IN_SELECTION = 4;
	
	public final static int SINGLE_SELECTION = 0;
	
	private static final long serialVersionUID = -8281257908869934492L;
	
	protected boolean selected;
	
	protected int selectionMode;
	
	/**
	 * 
	 */
	public CheckNode() {
	
		this(null);
	}
	
	/**
	 * @param userObject
	 */
	public CheckNode(final Object userObject) {
	
		this(userObject, true, false);
	}
	
	/**
	 * @param userObject
	 * @param allowsChildren
	 * @param selected
	 */
	public CheckNode(final Object userObject, final boolean allowsChildren, final boolean selected) {
	
		super(userObject, allowsChildren);
		
		this.selected = selected;
		
		setSelectionMode(DIG_IN_SELECTION);
	}
	
	/**
	 * @return selectionMode
	 */
	public int getSelectionMode() {
	
		return selectionMode;
	}
	
	/**
	 * @return selected
	 */
	public boolean isSelected() {
	
		return selected;
	}
	
	/**
	 * @param selected
	 */
	public void setSelected(final boolean selected) {
	
		this.selected = selected;
		
		//
		if(selectionMode == DIG_IN_SELECTION && children != null) {
			
			final Enumeration<?> enumm = children.elements();
			
			//
			while(enumm.hasMoreElements()) {
				
				final CheckNode node = (CheckNode)enumm.nextElement();
				
				node.setSelected(selected);
			}
		}
	}
	
	/**
	 * @param mode
	 */
	public void setSelectionMode(final int mode) {
	
		selectionMode = mode;
	}
	
	/**
	 * If you want to change "isSelected" by CellEditor
	 */
	// public void setUserObject(Object object) {
	//
	// //
	// if(object instanceof Boolean) {
	//
	// setSelected(((Boolean)object).booleanValue());
	//
	// }else {
	//
	// super.setUserObject(object);
	// }
	// }
}
